<cfmodule template="../../tag/layout.cfm">
<cfinclude template="../../inc/lib.cfm">
<cfscript>
	if( structKeyExists(rc, "categoryid")  && rc.categoryid > 0 ){
		categoryid = rc.categoryid;
	}else{
		categoryid = 0;
	}
	
	if( structKeyExists(rc, "categorytypeid")  && rc.categorytypeid > 0 ){
		categorytypeid = rc.categorytypeid;
	}else{
		categorytypeid = 0;
	}
</cfscript>
<!--- 表格显示 --->
<table id="grid"></table>
<div id="toolbar" style="display: none;">
	<form id="searchForm">
		<table>
			<tr>
				<td>标题</td>
				<td><input name="subject" style="width: 180px;" /></td>
				<td>创建时间</td>
				<td>
					<input name="created_ge"  class="easyui-datebox" style="width:100px"/>
					-
					<input name="created_le"  class="easyui-datebox" style="width:100px"/>
				</td>
				<td>
					<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'ext-icon-zoom',plain:true" onclick="grid.datagrid('load',fw.serializeObject($('#searchForm')));">过滤</a>
					<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'ext-icon-zoom_out',plain:true" onclick="$('#searchForm input').val('');grid.datagrid('load',{});">重置过滤</a>
				</td>
			</tr>
		</table>
	</form>
	<table>
		<tr>
			<cfif havePermission( queryStringDeleteVar('categoryid',QueryStringChangeVar('action',getCurrentPageType() & '.save') )  )>
			<td>
				<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'ext-icon-note_add',plain:true" onclick="addFun();">添加</a>
			</td>
			</cfif>
			
			<td>
				
				
				<cfif havePermission( queryStringDeleteVar('categoryid',QueryStringChangeVar('action',getCurrentPageType() & '.publish') )  )>
		        <a href="#" class="easyui-menubutton" data-options="menu:'#mm1',iconCls:'ext-icon-key',plain:true">批量发布</a>
		        <div id="mm1">
			    	<div data-options="iconCls:'icon-redo'" onclick="BatFun('publishbat',1);">批量发布</div>
			        <div data-options="iconCls:'icon-undo'" onclick="BatFun('publishbat',0);">取消发布</div>
			    </div>
				</cfif>
		        
			</td>
			<td><a href="?action=ads.categorys&categorytypeid=<cfoutput>#categorytypeid#</cfoutput>" class="easyui-linkbutton" data-options="iconCls:'icon-back',plain:true">返回</a></td>
			
		</tr>
	</table>
</div>
</cfmodule>
<script type="application/javascript">
//删除操作
var removeFun = function(id) {
    parent.$.messager.confirm('询问', '您确定要删除此记录？',
    function(r) {
        if (r) {
            $.post('?action=ads.delete', {
                id: id
            },
            function(result) {
                grid.datagrid('reload');
            },
            'json');
        }
    });
};

//单条记录操作
var singleFun = function(type,id) {
	var url = "?action=ads." + type;
	$.post(url, 
	{id:id},
	function() { 
		grid.datagrid('reload'); 
	},'json');
};

//批量记录操作
var BatFun = function(type,v){
	var url = "?action=ads." + type;
	var rows = $('#grid').datagrid('getSelections');
	var ids = [];
	for(var i=0; i<rows.length; i++){
		ids.push(rows[i].id);
	}
	if(ids.length >0){
		$.post(url, 
		{ids:ids.toString(),value:v},
		function() { 
			grid.datagrid('reload'); 
		},'json');
	}else{
		 $.messager.show({title:'提示',msg:'请选择操作记录！',timeout:2000,showType:'slide'});
	}
		
}

//查看操作
var showFun = function(id) {
    var dialog = parent.fw.modalDialog({
        title: '查看记录',
        url: '?action=ads.form&categoryid=<cfoutput>#categoryid#</cfoutput>&id=' + id,
        buttons: [{
            text: '关闭窗口',
            handler: function() {
                dialog.find('iframe').get(0).contentWindow.closeDialog(dialog);
            }
        }]
    });
};
//新增操作
var addFun = function() {
    var dialog = parent.fw.modalDialog({
        title: '新增记录',
        url: '?action=ads.form&categoryid=<cfoutput>#categoryid#</cfoutput>',
        buttons: [
        {
            text: '新增记录',
            handler: function() {
                dialog.find('iframe').get(0).contentWindow.submitForm(dialog, grid, parent.$);
            }
        },{
            text: '关闭窗口',
            handler: function() {
               dialog.find('iframe').get(0).contentWindow.closeDialog(dialog);
            }
        }]
    });
}
//修改操作
var editFun = function(id) {
    var dialog = parent.fw.modalDialog({
        title: '修改记录',
        url: '?action=ads.form&categoryid=<cfoutput>#categoryid#</cfoutput>&id=' + id,
        buttons: [
        {
            text: '修改记录',
            handler: function() {
                dialog.find('iframe').get(0).contentWindow.submitForm(dialog, grid, parent.$);
            }
        },{
            text: '关闭窗口',
            handler: function() {
                dialog.find('iframe').get(0).contentWindow.closeDialog(dialog);
            }
        }]
    });
};

$(function(){
	//列表操作
	var gird;
	grid = $('#grid').datagrid({
		fit: true,
		border:false,
		collapsible:false,
		pagination:true,
		singleSelect:false,
		rownumbers:true,
		sortName:'id',
		idField:'id',
		checkOnSelect:false,
		sortOrder:'desc',
		toolbar : '#toolbar',
		url:'?action=ads.list&categoryid=<cfoutput>#categoryid#</cfoutput>',
		columns:[[
			{field:'ck',checkbox:true},
			{field:'id',title:'ID',width:40,sortable:true},
			{field:'subject',title:'标题',width:400,sortable:true},
			{field:'type',title:'广告类型',width:70,sortable:true, align:'center',
				formatter:function(value,row){
					var str = '';
					if(value == 0){
						str = "文本类型";
					}else{
						str = "图片类型";
					}
					return str;
				}
			},
			{field:'materialids',title:'物料数量',width:70, align:'center',
				formatter:function(value,row){
					return value.split(',').length;
				}
			},
			{field:'ispublish',title:'发布',width:50,sortable:true,align:'center',
				formatter:function(value,row){
					var str = '';
					if(!value){
						str = "<span style='color:red'>未发布</span>";
					}
					return str;
				}
			},
			{field:'expiry',title:'过期时间',width:80,align:'center',sortable:true},
			{field:'created',title:'添加时间',width:80,align:'center',sortable:true},
			{field:'edited',title:'修改时间',width:80,align:'center',sortable:true}, 
			{
				title : '操作',
				field : 'action',
				width : '160',
				formatter : function(value, row) {
					var str = '';
						<cfif havePermission( queryStringDeleteVar('categoryid',QueryStringChangeVar('action',getCurrentPageType() & '.form') )  )>
						str += fw.formatString('<img class="iconImg ext-icon-note" title="查看" onclick="showFun(\'{0}\');"/>', row.id);
						</cfif>
						
						<cfif havePermission( queryStringDeleteVar('categoryid',QueryStringChangeVar('action',getCurrentPageType() & '.update') )  )>
						str += fw.formatString('<img class="iconImg ext-icon-note_edit" title="编辑" onclick="editFun(\'{0}\');"/>', row.id);
						</cfif>
						
						<cfif havePermission( queryStringDeleteVar('categoryid',QueryStringChangeVar('action',getCurrentPageType() & '.delete') )  )>
						str += fw.formatString(' <img class="iconImg ext-icon-note_delete" title="删除" onclick="removeFun(\'{0}\');"/>', row.id);
						</cfif>
						
						<cfif havePermission( queryStringDeleteVar('categoryid',QueryStringChangeVar('action',getCurrentPageType() & '.publish') )  )>
						str += fw.formatString(' <img class="iconImg ext-icon-key" title="发布" onclick="singleFun(\'publish\',{0});"/>', row.id);
						</cfif>
						
					return str;
				}
			} 
		]],
		onLoadSuccess : function(data) {
			$('.iconImg').attr('src', fw.pixel_0);
		}
	});
	
	
});
</script>